//
//  StateProgress.m
//  Koina
//
//  Created by welldone on 13-12-13.
//  Copyright (c) 2013年 net.koina. All rights reserved.
//

#import "StateProgress.h"

@interface StateProgress ()
@property (nonatomic,retain)UIImageView *bottomImgView;
@property (nonatomic,retain)UIView *pView,*progressView;
@property (nonatomic,retain)UILabel *tipLab;
-(void)upFrame;
@end


@implementation StateProgress
@synthesize bottomImgView,progressView,tipLab,pView;



- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        pView = [[UIView alloc]initWithFrame:CGRectMake(frame.size.width/2-170/2, 0, 170, 0)];
        bottomImgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 170, 0)];
        progressView = [[UIView alloc]initWithFrame:CGRectMake(-170, 0, 170*3,0)];
        [pView addSubview:progressView];
        [pView addSubview:bottomImgView];
        [pView setClipsToBounds:YES];
        
        
        tipLab = [[UILabel alloc]initWithFrame:CGRectZero];
        [tipLab setText:@"加载中..."];
        [tipLab setTextAlignment:NSTextAlignmentCenter];
        [tipLab setBackgroundColor:[UIColor clearColor]];
        [tipLab setFont:[UIFont systemFontOfSize:13.0f]];
        [tipLab setTextColor:[UIColor grayColor]];

        
        [self addSubview:pView];
        [self addSubview:tipLab];
        [self setClipsToBounds:YES];
        
        pView.hidden = YES;
        tipLab.hidden = YES;
    }
    return self;
}



-(void)setProgressImg:(NSString*)progressImgName withBackImg:(NSString*)backImgName{
    
    UIImage *bgImg = [UIImage imageNamed:backImgName];
    CGRect bufRect = progressView.frame;
    bufRect.size.height = bgImg.size.height-1;
    [progressView setFrame:bufRect];
    
    bufRect = bottomImgView.frame;
    bufRect.size.height = bgImg.size.height-1;
    [bottomImgView setFrame:bufRect];
    
    bufRect = pView.frame;
    bufRect.size.height = bottomImgView.frame.size.height;
    [pView setFrame:bufRect];
    
    
    bgImg  = [bgImg stretchableImageWithLeftCapWidth:bgImg.size.width/2 topCapHeight:bgImg.size.height/2];
    [bottomImgView setImage:bgImg];
    [progressView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:progressImgName]]];
    
    [tipLab setFrame:CGRectMake(0,progressView.frame.size.height+progressView.frame.origin.y+3,self.frame.size.width,self.frame.size.height/2)];
}





-(void)loadData{
    [self loadDataWithTip:@"加载中..."];
}


-(void)loadDataWithTip:(NSString*)tip{
    
    [tipLab setText:tip];
    pView.hidden = NO;
    tipLab.hidden = NO;
    [self upFrame];
}



-(void)noData{
    
    pView.hidden = YES;
    [tipLab setHidden:NO];
    [tipLab setText:@"没有数据"];
    [progressView.layer removeAllAnimations];
}



-(void)netError{
    
    pView.hidden = YES;
    [tipLab setText:@"网络出错!"];
    [progressView.layer removeAllAnimations];
}


-(void)setCustTip:(NSString*)tips{
    
    pView.hidden = YES;
    [tipLab setText:tips];
    [progressView.layer removeAllAnimations];
}

-(void)upFrame{
    
    [progressView .layer removeAllAnimations];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:2.8f];//设置动画持续时间
    [UIView setAnimationRepeatCount:99999999];//设置动画重复次数
    CGRect bufRect = progressView.frame;
    if (bufRect.origin.x>=0) {
        bufRect.origin.x = -bottomImgView.frame.size.width;
    }else{
        bufRect.origin.x=0;
    }
    progressView.frame = bufRect;
    [UIView commitAnimations]; //设置动画的动作，该动画就是实现myCardView向左滑动320个像素
}

/*
 UIViewAnimationCurveEaseInOut  //开始和结束时动画效果比较慢
 UIViewAnimationCurveEaseIn       //开始动画效果比较慢
 UIViewAnimationCurveEaseOut     //结束动画效果比较慢
 UIViewAnimationCurveLinear         //平滑的动画效果
 */
@end
